const { expressjwt: jwt } = require("express-jwt");
const { BASE_PATH } = process.env;

const { JWT_KEY } = process.env;
module.exports = ({
  unlessPath = ["/login", "/init"].map((el) => `${BASE_PATH}${el}`),
  maxAge = "2 days",
} = {}) => [
  jwt({ secret: JWT_KEY, algorithms: ["HS256"] }, { maxAge }).unless({
    path: unlessPath,
  }),
  (error, req, res, next) => {
    if (error.name === "UnauthorizedError") {
      res.status(401).send({
        status: 401,
        message: error.inner.message,
        message: "Invalid token",
      });
    }
  },
  (req, res, next) => {
    next();
  },
];
